package com.cszjzx.member.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.cszjzx.member.po.Field;
import com.cszjzx.member.po.Member;
import com.cszjzx.member.po.Stage;
import com.cszjzx.member.service.FieldService;
import com.cszjzx.member.service.FieldServiceImpl;
import com.cszjzx.member.service.MemberService;
import com.cszjzx.member.service.MemberServiceImpl;
import com.cszjzx.member.service.StageService;
import com.cszjzx.member.service.StageServiceImpl;
import com.cszjzx.member.util.QRCode;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class StuLoginCheckAction extends ActionSupport {

	private String name;
	private String password;
	private String message="";
	private int module;
	private String[] s,s1,s2,s3,s4;
	private int process;
	private int sex;	//性别判断，0为女，1为男
	private String ss="";
	MemberService memberservice = new MemberServiceImpl();
	HttpServletRequest request = ServletActionContext.getRequest();
	StageService stageService = new StageServiceImpl();
	FieldService fieldService = new FieldServiceImpl();
	QRCode qrcode;


	public int getSex() {
		return sex;
	}

	public void setSex(int sex) {
		this.sex = sex;
	}

	public String[] getS1() {
		return s1;
	}

	public void setS1(String[] s1) {
		this.s1 = s1;
	}

	public String[] getS2() {
		return s2;
	}

	public void setS2(String[] s2) {
		this.s2 = s2;
	}

	public String[] getS3() {
		return s3;
	}

	public void setS3(String[] s3) {
		this.s3 = s3;
	}

	public String[] getS4() {
		return s4;
	}

	public void setS4(String[] s4) {
		this.s4 = s4;
	}

	public int getProcess() {
		return process;
	}

	public void setProcess(int process) {
		this.process = process;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public int getModule() {
		return module;
	}

	public void setModule(int module) {
		this.module = module;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public String[] getS() {
		return s;
	}

	public void setS(String[] s) {
		this.s = s;
	}
	
	public void setPassword(String password) {
		this.password = password;
	}
	public String person(Member member,int process){
		
		List<Stage> stages = (List<Stage>)stageService.getStageList();
		List<Field> fields = (List<Field>)fieldService.getFieldList();
		
		setS(new String[stages.size()-1]);
		
		int a1=0,a2=0,a3=0,a4=0;
		for(int i=0;i<fields.size();i++){
			if(fields.get(i).getStage() == 1){
				a1++;
			}
			else if(fields.get(i).getStage() == 2){
				a2++;
			}
			else if(fields.get(i).getStage() == 3){
				a3++;
			}
			else if(fields.get(i).getStage() == 4){
				a4++;
			}
		}
		s1 = new String[a1];
		for(int i=0,j=0;i<fields.size()&&j<a1;i++){
			if(fields.get(i).getStage() == 1){
				String t;
				if(member.getValue(fields.get(i).getChinese())== null){
					t = "无";
				}
				else{
					t = member.getValue(fields.get(i).getChinese());
				}
				String s = fields.get(i).getChinese()+":"+t;
				s1[j] = s;
				ss+=s1[j]+"\n";
				j++;	
				
			}
			
		}
		if(process>=2){
			s2 = new String[a2];
			for(int i=0,j=0;i<fields.size()&&j<a2;i++){
				if(fields.get(i).getStage() == 2){
					String t;
					if(member.getValue(fields.get(i).getChinese())== null){
						t = "无";
					}
					else{
						t = member.getValue(fields.get(i).getChinese());
					}
					String s = fields.get(i).getChinese()+":"+t;
					s2[j] = s;
					ss+=s2[j]+"\n";
					j++;
					
				}
			}
		}
		if(process>=3){
			s3 = new String[a3];
			for(int i=0,j=0;i<fields.size()&&j<a3;i++){
				if(fields.get(i).getStage() == 3){
					String t;
					if(member.getValue(fields.get(i).getChinese())== null){
						t = "无";
					}
					else{
						t = member.getValue(fields.get(i).getChinese());
					}
					String s = fields.get(i).getChinese()+":"+t;
					s3[j] = s;
					ss+=s3[j]+"\n";
					j++;
					
				}
			}
		}
		if(process>=4){
			s4 = new String[a4];
			for(int i=0,j=0;i<fields.size()&&j<a4;i++){
				if(fields.get(i).getStage() == 4){
					String t;
					if(member.getValue(fields.get(i).getChinese())== null){
						t = "无";
					}
					else{
						t = member.getValue(fields.get(i).getChinese());
					}
					String s = fields.get(i).getChinese()+":"+t;
					s4[j] = s;
					ss+=s4[j]+"\n";
					j++;
					
				}
			}
		}
		return ss;
	}
	/**
	 * 登录验证
	 * 输入数据的合法性在前台进行验证，比如是否为空等
	 */
	public String execute() throws Exception{
		
		String ret = "login";
		Member member;
		if(name==null||name.equals("")){
			message="请输入用户名！";
		}else{
			member = memberservice.getMemberByIdcard(name);
			try {
				if(member!=null&&password.equals(member.getValue("password"))){
					request.getSession().setAttribute("student", member);
					if(module==4){
						ret = "stuLogin";
					}else{
						String realPath = ServletActionContext.getServletContext().getRealPath("QRCode");
						String savePath = realPath + "\\";
						String sexString = member.getValue("sex");
						if(sexString.equals("男")){
							sex = 1;
						}
						else{
							sex = 0;
						}
						process=Integer.parseInt(member.getValue("process"));
						String string = person(member,process);
						qrcode = new QRCode();
						qrcode.create_image(string,savePath,name);
						ret = "info";
					}
				}else{
						message="用户账号或密码错误！";
						ret =  "login";
					}
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}
		return ret;
	}
}
